﻿(function (wtm) {
    $('body').on('click', '.wtm-upload-del', function () {
        var $el = $(this);
        var $form = $el.parents('form');
        var inputId = $el.attr('input-id');
        var fileId = $('#' + inputId).val();
        $('#' + inputId).val(null);
        $form.append("<input type='hidden' name ='DeletedfileIds' value = '" + fileId + "'/> ");
        $el.parent().remove();
    });

    wtm.upload_Render = function (para) {

        if (para.beforeMount.length > 0) {
            eval(para.beforeMount + '(para);');
        }

        /* obj.id
         * obj.filesize
         * obj.ext
         * obj.SysUploadFailed
         * obj.ShowPreview
         * obj.PreviewWidth
         * obj.PreviewHeight
         * obj.cs
         * obj.SysDelete
         */
        var index = 0;
        var $div = $('#' + para.id + 'div');
        /* 普通图片上传 */
        layui.upload.render({
            elem: '#' + para.id + 'button'
            , url: para.url
            , size: para.fileSize
            , accept: 'file'
            , exts: para.ext
            , xhr: xhrOnProgress
            , progress: function (value) {
                $('.layui-progress .layui-progress-bar').css('width', value + '%');
            }
            , before: function (beforeObj) {
                index = layui.layer.load(2);
                para.previewObj = beforeObj;
            }
            , done: function (res) {
                layui.layer.close(index);

                if (res.Data.Id === '') {
                    // 如果文件为空
                    $div.empty();
                    layui.layer.msg(para.sysUploadFailed);
                    return;
                }

                $('#' + para.id).val(res.Data.Id);

                // 是否预览
                if (para.showPreview) {
                    var img_tpl = $('#wtmtpl-upload-img').html();
                    var option = {
                        id: para.id,
                        fileId: res.Data.Id,
                        alt: res.Data.Name,
                        cs: para.cs,
                        width: para.previewWidth,
                        height: para.previewHeight,
                        disabled: para.disabled
                    };

                    layui.laytpl(img_tpl).render(option, function (str) {
                        $div.html(str);
                    });

                    layui.layer.photos({
                        photos: '#' + para.id + 'div'
                        , anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                    });
                } else {
                    var file_tpl = $('#wtmtpl-upload-file').html();
                    var option = {
                        id: para.id,
                        fieldName: para.fieldName,
                        fileId: res.Data.Id,
                        fileName: res.Data.Name,
                        sysDelete: para.sysDelete,
                        disabled: para.disabled
                    };
                    layui.laytpl(file_tpl).render(option, function (str) {
                        $div.html(str);
                    });
                }

            }
            , error: function () {
                layui.layer.close(index);
            }
        });

        if (para.mounted.length > 0) {
            eval(para.mounted + '(para);');
        }

        var xhrOnProgress = function (fun) {
            xhrOnProgress.onprogress = fun; //绑定监听
            //使用闭包实现监听绑
            return function () {
                var xhr = $.ajaxSettings.xhr();
                //判断监听函数是否为函数
                if (typeof xhrOnProgress.onprogress !== 'function')
                    return xhr;
                if (xhrOnProgress.onprogress && xhr.upload) {
                    xhr.upload.onprogress = xhrOnProgress.onprogress;
                }
                return xhr;
            }
        }
    };

    wtm.upload_AjaxGet = function (obj) {
        /* obj.geturl 获取附件的url
         * obj.picurl 获取图片的url
         * obj.id input的id
         * obj.ShowPreview 是否显示预览
         * obj.Disabled 是否禁用
         * obj.data ajax返回的数据
         * obj.PreviewWidth 预览宽度
         * obj.PreviewHeight 预览高度
         * obj.fileId 文件的id
         * obj.cs
         * obj.downloadurl 文件的下载url
         * obj.SysDelete 删除提示语
         */
        var $div = $('#' + obj.id + 'div');
        $.ajax({
            cache: false,
            type: 'GET',
            url: obj.geturl,
            async: true,
            success: function (data) {
                if (obj.showPreview === true) {
                    var img_tpl = $('#wtmtpl-upload-img').html();
                    var option = {
                        id: obj.id,
                        fileId: obj.fileId,
                        alt: data,
                        cs: obj.cs,
                        width: obj.previewWidth,
                        height: obj.previewHeight,
                        disabled: obj.disabled
                    };

                    layui.laytpl(img_tpl).render(option, function (str) {
                        $div.html(str);
                    });

                    layui.layer.photos({
                        photos: '#' + obj.id + 'div'
                        , anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                    });
                } else {
                    var file_tpl = $('#wtmtpl-upload-file').html();
                    var option = {
                        id: obj.id,
                        fileId: obj.fileId,
                        fileName: data,
                        sysDelete: obj.sysDelete,
                        disabled: obj.disabled
                    };
                    layui.laytpl(file_tpl).render(option, function (str) {
                        $div.html(str);
                    });
                }
            }
        });
    };
})(wtm);